$(document).ready(function () {
    var likes = $(".like");
    $.each(likes, function (index, value) {
        if (value.getAttribute('data-status') === "1") {
            value.getElementsByTagName("img")[0].setAttribute('src', '/static/movie/images/like-on.png')
        }
    });
    $("#review-reply").click(function () {
        var review_id = $(".review-id").val();
        var user_id = $(".r_user-id").val();
        var content = $(".comment-content").val();
        if (content.length <= 0 || content.length >= 100) {
            $(".comment-content").popover('toggle');
            return false
        }
        console.log(review_id);
        console.log(user_id);
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/post/review_info/commentverify/",
            data: {"review-id": review_id, "user-id": user_id, "comment-content": content},
            success: function (message) {
                if (message.state === 'success') {
                    window.location.reload(true)
                }
                else {
                    console.log('fail');
                }
            },
            error: function (message) {
                alert("fail");
                window.location.reload(true)
            }
        })
    });

    $(".content-action").on("click", ".like", function () {
        var lid = $(this).attr("data-like");
        var rid = $(this).attr("review-id");
        var uid = $(this).children(".user-id").text();
        var status = $(this).attr("data-status");
        var oldTotal = parseInt($(this).children(".zan").text());
        var newTotal = 0;
        if (status === "1") { //是状态=1奥！
            newTotal = oldTotal - 1;
            alert("-1的操作");
            $(this).children(".zan").text((newTotal === 0) ? "0" : newTotal);
            $(this).attr("data-status", "0");
            $(this).children('img').attr('src', '/static/movie/images/like-off.png')
        } else {
            newTotal = oldTotal + 1;
            alert("+1的操作");
            $(this).children(".zan").text(newTotal);
            $(this).attr("data-status", "1");
            $(this).children('img').attr('src', '/static/movie/images/like-on.png')

        }
        $.ajax({
            url: "/post/review_info/praiseverify/",
            data: {"praise-id": lid, "review-id": rid, "user-id": uid, "status": status},
            dataType: "json",
            type: "POST",
            success: function (data) {
                alert("success");
            },
            error: function () {
                alert('fail')
            }
        })
    });
});